package com.example.webchar.webchar.controller;

import com.example.webchar.webchar.service.NovelService;
import com.example.webchar.webchar.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @program: webchars
 * @description
 * @author: 毛叶贵子
 * @create: 2021-08-27 20:42
 **/
@CrossOrigin
@RestController
@RequestMapping("/search")
public class NovelController {
    @Autowired
    private NovelService novelService;


    /**
     * 模糊小说的名称或者作者名称 模糊查询小说
     *
     * @param bookName
     * @return
     */
    @GetMapping("/search/{bookName}")
    public Result fuzzyQueryNovel(@PathVariable String bookName) {
        return bookName.equals(null) ? Result.fail().add("err", "请输入书本名称") : Result.success().add("success", novelService.fuzzyQueryNovel(bookName));
    }

    /**
     * 根据小说的类型返回小说信息
     *
     * @param novelType
     * @return
     */
    @GetMapping("/type/{novelType}")
    public Result queryTheTypeOfNovel(@PathVariable("novelType") String novelType) {
        return novelType.equals(null) ? Result.fail().add("err", "请选择小说类型") : Result.success().add("success", novelService.queryNovelType(novelType));
    }

    /**
     * 主界面 返回各种合集()
     *
     * @return
     */
    @GetMapping("/index")
    public Result queryNovelIndex() {
        try {
            return Result.success().add("success", novelService.queryNovelIndex());
        } catch (Exception e) {
            return Result.fail().add("err", "出现错误");
        }
    }

    /**
     * 根据作者名称返回当前作者的所有小说
     *
     * @param name
     * @return
     */

    @GetMapping("/queryNovelByName/{name}")
    public Result queryNovelByName(@PathVariable("name") String name) {
        return name.equals(null) ? Result.fail() : Result.success().add("success", novelService.queryNovelByName(name));
    }

}
